package cc.vv.btpartyjob.partymanager.activity

import android.support.v4.widget.SwipeRefreshLayout
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.view.BTLoadMoreView
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.enums.H5UrlEnum
import cc.vv.btpartyjob.jnimodule.server.JniStartActivity
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.adapter.UnitIntroductionAdapter
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.UnitIntroductionActivityDelegate
import cc.vv.btpartyjob.partymanager.moudel.bean.UnitJJBean
import cc.vv.btpartyjob.partymanager.moudel.resp.UnitJJResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 单位简介
 * Created by Chen.qi on 2018/10/29.
 */
class UnitIntroductionActivity : BaseActivityMVP<UnitIntroductionActivityDelegate>()
        , BaseQuickAdapter.OnItemClickListener,
        SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener {

    /**
     * 适配器
     */
    private var mAdapter: UnitIntroductionAdapter? = null

    /**
     * 当前页数
     */
    private var pageNo: Int = 1
    /**
     * 每页条数
     */
    private val pageSize: Int = 10

    /**
     * 集合数据
     */
    private var mDataList: ArrayList<UnitJJBean>? = null

    override fun initData() {
        viewDelegate?.setTopBarTitle("单位简介")
        mDataList = ArrayList()
        if (mAdapter == null) {
            mAdapter = UnitIntroductionAdapter(R.layout.item_street_meeting)
            viewDelegate?.setRecyclerViewAdapter(mAdapter)
            mAdapter?.setLoadMoreView(BTLoadMoreView())
            viewDelegate?.setAdapterData(mAdapter, mDataList)
        }
        PartyManagerServer.unitIntroduceList(pageNo, pageSize, true)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })

        mAdapter?.onItemClickListener = this
        viewDelegate?.bindRefreshListener(this)
        viewDelegate?.bindLoadMore(mAdapter, this)
        mAdapter?.disableLoadMoreIfNotFullPage()
    }

    override fun getDelegateClass(): Class<UnitIntroductionActivityDelegate> {
        return UnitIntroductionActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
//        JniStartActivity.startIntentWebActivity(this, "单位简介", "http://www.hao123.com")
        JniStartActivity.startIntentWebActivity(mContext, H5UrlEnum.单位简介详情, mDataList?.get(position)?.id)
    }


    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is UnitJJResponseObj -> {
                viewDelegate?.stopRefreshing()
                if (null == obj.data) {
                    return
                }
                if (1 == pageNo) {
                    mDataList?.clear()
                    if (null != obj.data.records) {
                        mDataList!!.addAll(obj.data.records)
                    }
                    viewDelegate?.setAdapterData(mAdapter, mDataList)
                    mAdapter?.disableLoadMoreIfNotFullPage()
                } else {
                    if (mAdapter?.data?.size!! >= obj.data.total) {
                        //数据全部加载完毕
                        mAdapter?.loadMoreEnd(true)
                        mAdapter?.loadMoreEnd()
                    } else {
                        //成功获取更多数据
                        if (null != obj.data.records) {
                            mDataList!!.addAll(obj.data.records)
                        }
                        mAdapter?.setNewData(mDataList)
                        //加载数据
                        mAdapter?.disableLoadMoreIfNotFullPage()
                        mAdapter?.loadMoreComplete()
                    }
                }
            }
        }
    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
        mAdapter?.loadMoreComplete()
        mAdapter?.loadMoreFail()
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
    }

    override fun onRefresh() {
        mAdapter?.setEnableLoadMore(false)//这里的作用是防止下拉刷新的时候还可以上拉加载
        pageNo = 1
        PartyManagerServer.unitIntroduceList(pageNo, pageSize, true)
    }

    override fun onLoadMoreRequested() {
        if (mDataList!!.size < 10) {
            mAdapter?.loadMoreComplete()
            mAdapter?.loadMoreEnd(true)
            return
        }
        pageNo++
        PartyManagerServer.unitIntroduceList(pageNo, pageSize, true)
    }

}